Spring Boot-এ ResponseEntity একটি গুরুত্বপূর্ণ ক্লাস যা HTTP রেসপন্সের স্ট্যাটাস কোড, হেডার, এবং বডি পরিচালনা করতে ব্যবহৃত হয়। HTTP Methods এর মাধ্যমে ResponseEntity হ্যান্ডল করার পদ্ধতি নিম্নে আলোচনা করা হলো:
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> getDataFromApi() {
String url = "https://jsonplaceholder.typicode.com/posts";
// API থেকে ResponseEntity গ্রহণ করা
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return response;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@GetMapping("/getPosts")
public ResponseEntity<String> getPosts() {
return apiService.getDataFromApi();
}
}
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> postDataToApi(String jsonPayload) {
String url = "https://jsonplaceholder.typicode.com/posts";
HttpEntity<String> request = new HttpEntity<>(jsonPayload);
// POST Method-এর মাধ্যমে ডেটা পাঠানো
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
return response;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@PostMapping("/createPost")
public ResponseEntity<String> createPost(@RequestBody String jsonPayload) {
return apiService.postDataToApi(jsonPayload);
}
}
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> updateDataInApi(String jsonPayload, int id) {
String url = "https://jsonplaceholder.typicode.com/posts/" + id;
HttpEntity<String> request = new HttpEntity<>(jsonPayload);
// PUT Method ব্যবহার
restTemplate.put(url, request);
return new ResponseEntity<>("Resource updated successfully", HttpStatus.OK);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@PutMapping("/updatePost")
public ResponseEntity<String> updatePost(@RequestBody String jsonPayload, @RequestParam int id) {
return apiService.updateDataInApi(jsonPayload, id);
}
}
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> deleteDataFromApi(int id) {
String url = "https://jsonplaceholder.typicode.com/posts/" + id;
// DELETE Method ব্যবহার
restTemplate.delete(url);
return new ResponseEntity<>("Resource deleted successfully", HttpStatus.OK);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@DeleteMapping("/deletePost")
public ResponseEntity<String> deletePost(@RequestParam int id) {
return apiService.deleteDataFromApi(id);
}
}
Custom HTTP Status Code:
return new ResponseEntity<>("Error occurred", HttpStatus.BAD_REQUEST);
Custom HTTP Headers:
HttpHeaders headers = new HttpHeaders();
headers.set("Custom-Header", "HeaderValue");
return new ResponseEntity<>("Response Body", headers, HttpStatus.OK);
ResponseEntity.ok() Shortcut:
return ResponseEntity.ok("Response Body");
ResponseEntity.noContent() for DELETE:
return ResponseEntity.noContent().build();
Spring Boot-এ ResponseEntity ব্যবহার করে HTTP Methods এর জন্য রেসপন্স বডি, হেডার, এবং স্ট্যাটাস কোড সহজেই হ্যান্ডল করা যায়। এটি অ্যাপ্লিকেশনকে আরো লচিৎ এবং কাস্টমাইজড রেসপন্স প্রদান করতে সহায়তা করে।
যদি আরো বিস্তারিত সাহায্য প্রয়োজন হয়, জানান! 😊
Read more